package cn.com.xinjinfu.pdoulcProduct.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct.TransferProductInfo;
import cn.com.xinjinfu.pdoulcProduct.activity.login.CheckMobileActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.recharge.BuyActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.user.ActCertification;
import cn.com.xinjinfu.pdoulcProduct.entity.Page;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.L;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;


/**
 * Created by android-adt on 2016/4/5.
 */
public class BAdapter extends MyBaseAdapter {


    private Context mContext;
    private LayoutInflater inflater;
    private Page page;
    private List<Page> myList;

    public BAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        myList = new ArrayList<>();
        this.mContext = context;
    }
    public void setMyList(List list){
        myList.clear();
        myList.addAll(list);
        notifyDataSetChanged();
    }
    /**
     * 分页数据加载到listview
     * @param list 数据list
     */
    @Override
    public void addList(List list) {
        myList.addAll(list);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return myList == null ? 0 :myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

			/* 绑定相应的视图 */
            convertView = inflater.inflate(R.layout.itemj_fixed_assignment, null);
            holder.mBidTitle = (TextView) convertView.findViewById(R.id.tv_bid_title);
            holder.tv_day_time = (TextView) convertView.findViewById(R.id.tv_day_time);
            holder.item = (LinearLayout)convertView.findViewById(R.id.item);
            holder.mNterestRate = (TextView) convertView.findViewById(R.id.tv_nterest_rate);
            holder.tv_yuqi_shouyilv = (TextView) convertView.findViewById(R.id.tv_yuqi_shouyilv);
            holder.tv_credit_rate = (TextView) convertView.findViewById(R.id.tv_credit_rate);
            holder.tv_assignment_rate = (TextView) convertView.findViewById(R.id.tv_assignment_rate);
            holder.tv_zhe_rang = (TextView) convertView.findViewById(R.id.tv_zhe_rang);
            holder.tv_shengyu_fenshu = (TextView) convertView.findViewById(R.id.tv_shengyu_fenshu);

            holder.mMairu = (Button) convertView.findViewById(R.id.bt_mairu);
            holder.mQiangGuang = (Button) convertView.findViewById(R.id.bt_qiangguang);
            holder.mHuanKuan = (Button) convertView.findViewById(R.id.bt_huankuan);
            holder.mOver = (Button) convertView.findViewById(R.id.bt_over);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

         page = myList.get(position);
        L.d("TransferorFragmentBean--------------------------------" + page.toString());
        if (page != null) {
            holder.mBidTitle.setText(page.getBidTitle());
            holder.mMairu.setVisibility(View.VISIBLE);
            holder.tv_day_time.setText(page.getRemainderDay() + "");
            NumberFormat nbf = NumberFormat.getInstance();
            holder.mNterestRate.setText(nbf.format(page.getBidApr()) + "%");

            holder.tv_credit_rate.setText(TextViewUtil.getSumLength(page.getPortionOriginalAmount()));
            holder.tv_assignment_rate.setText(TextViewUtil.getSumLength(page.getPortionRransferAmount()));
            holder.tv_shengyu_fenshu.setText((page.getTotalPortionNums()-page.getHaveBuyPortionNums())+"");
            if (page.getTransferPrice() == 1) {          //等额本息
                holder.tv_zhe_rang.setText("折让");
                double x = page.getDebtAmount() - page.getTransferPrice();
                holder.tv_yuqi_shouyilv.setText(TextViewUtil.getSumLength(x));
            } else {
                holder.tv_zhe_rang.setText("预期年化收益率(约)");
                holder.tv_yuqi_shouyilv.setText(nbf.format(page.getTransferApr()) + "%");
            }
        }

        //给Button添加单击事件  添加Button之后ListView将失去焦点  需要的直接把Button的焦点去掉
        holder.mMairu.setTag(position);
        holder.mMairu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                User user = AccountManager.getInstance().getUser();
                if (user != null && user.getCurrUser().isLogged()) {
                    if (user.getCurrUser().getIdNumber() == null) {
                        Intent intent0 = new Intent();
                        intent0.putExtra("cer_style_falg", 1);
                        intent0.setClass(mContext, ActCertification.class);
                        mContext.startActivity(intent0);
                        ToastManager.showShort(mContext, "请先进行实名认证！");
                    }else {
                        //跳转到买入界面
                        Intent intent = new Intent(mContext, BuyActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("objBuy", 2);
                        bundle.putDouble("shengyu", myList.get(position).getTransferPrice());
                        bundle.putString("Id", myList.get(position).getTransferId() + "");
                        bundle.putString("titlename", myList.get(position).getBidTitle());
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                } else {
                    mContext.startActivity(new Intent(mContext, CheckMobileActivity.class));
                }
            }
        });
        holder.item.setTag(position);
        holder.item.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //跳转到详情页面
                Intent intent = new Intent();
                intent.putExtra("investId", myList.get(position).getInvestId() + "");
                intent.putExtra("rettonStyle", "0");
                intent.putExtra("bidId", myList.get(position).getBidId());
                intent.putExtra("bidStatus", "转让中");
                mContext.startActivity(intent.setClass(mContext, TransferProductInfo.class));
            }
        });
        return convertView;
    }


    public void toast(String text) {
        Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
    }

    class ViewHolder {
        public TextView mBidTitle;
        public TextView tv_day_time;
        public LinearLayout item;
        public TextView mNterestRate;
        public TextView tv_yuqi_shouyilv;
        public TextView tv_credit_rate;
        public TextView tv_assignment_rate;
        public TextView tv_zhe_rang;
        public TextView tv_shengyu_fenshu;

        public Button mMairu;
        public Button mQiangGuang;
        public Button mHuanKuan;
        public Button mOver;
    }

}
